﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarDiscountDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarDiscountDB
    {
        public CarDiscountDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarDiscount cardiscount)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarDiscountInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCarDiscountId", cardiscount.ProviderCarDiscountId);
            _sPara.Add("@ProviderCompanyId", cardiscount.ProviderCompanyId);
            _sPara.Add("@DiscountName", cardiscount.DiscountName);
            _sPara.Add("@Description", cardiscount.Description);
            _sPara.Add("@Type", cardiscount.Type);
            _sPara.Add("@Rate", cardiscount.Rate);
            _sPara.Add("@RefCEUserId", cardiscount.RefCEUserId);
            _sPara.Add("@Status", cardiscount.Status == true ? 1 : 0);
            _sPara.Add("@Finfo1", cardiscount.FInfo1);
            _sPara.Add("@Dinfo1", cardiscount.DInfo1);
            #region Parameter
           
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public DataTable GetCarDiscountList(Int64 ProviderCompanyId, Int64 ProviderDiscountId)
        {
            DataTable _cclist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarDiscountSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@ProviderCarDiscountId", ProviderDiscountId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _cclist = _dt;
                }
            }
            return _cclist;
        }

        public DataTable GetCarDiscountList(Int64 ProviderCompanyId)
        {
            DataTable _cclist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarDiscountSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _cclist = _dt;
                }
            }
            return _cclist;
        }

        public CarDiscount GetCarDiscount(Int64 ProviderDiscountId)
        {
            CarDiscount _cardiscount = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarDiscountSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarDiscountId", ProviderDiscountId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _cardiscount = new CarDiscount();
                    _cardiscount = FillDataRecord(_dt, 0);
                }
            }
            return _cardiscount;
        }

        public CarDiscount GetCarDiscount(Int64 ProviderCompanyId, string DiscountName, string Description, string Type)
        {
            CarDiscount _cardiscount = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarDiscountSelectSingleItemSelected";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@DiscountName", DiscountName);
            _spara.Add("@Description", Description);
            _spara.Add("@Type", Type);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _cardiscount = new CarDiscount();
                    _cardiscount = FillDataRecord(_dt, 0);
                }
            }
            return _cardiscount;
        }

        private CarDiscount FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarDiscount myCarDiscount = new CarDiscount();
            myCarDiscount.ProviderCarDiscountId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarDiscountId"].ToString());
            myCarDiscount.ProviderCompanyId = Convert.ToInt32(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarDiscount.DiscountName = myDataTable.Rows[RowId]["DiscountName"].ToString();
            myCarDiscount.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myCarDiscount.Type = myDataTable.Rows[RowId]["Type"].ToString();
            myCarDiscount.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarDiscount.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarDiscount.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarDiscount.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarDiscount.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarDiscount.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarDiscount;
        }

        public bool Delete(Int64 ProviderCarDiscountId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarDiscountDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarDiscountId", ProviderCarDiscountId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }
    }
}
